package picPoll
{
	import com.event.EventZheng;
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class Poll extends Sprite
	{
		private var picArr:Array;
		private var titleArr:Array;
		private var contentArr:Array;
		
		private var pollNum:int;
		private var _currentNum:int=-1;//当前显示的图片在数组中的编号
		private var isFirst:Boolean=true;//读取的第一张图�?
		
		private var lightBall:ShowLightBall;
		private var lightBallY:int = 460;
		
		private var _txtTitle:TextField;
		private var _txtContent:TextField;
		
		private var txtTitleY:int=lightBallY+80;
		private var txtContentY:int=lightBallY+100;
		
		private var timer:Timer=new Timer(StaticInfo.PIC_SPACE,0);
		public function Poll()
		{
			var textFormat:TextFormat = new TextFormat("Arial", 18);
			var textFormat2:TextFormat = new TextFormat("Arial", 14);
			lightBall = new ShowLightBall(this);
			
			/*var bmd:BitmapData=new BitmapData(StaticInfo.PIC_WIDTH,StaticInfo.PIC_HEIGHT,false,0x999999);
			var bm:Bitmap=new Bitmap(bmd);
			addChild(bm);*/
						
			_txtTitle=new TextField();
			_txtContent=new TextField();
			_txtTitle.defaultTextFormat=textFormat;
			_txtContent.defaultTextFormat=textFormat2;
			//_txtTitle.setTextFormat(textFormat);
			//_txtContent.setTextFormat(textFormat2);
			_txtTitle.autoSize = TextFieldAutoSize.LEFT;
			_txtContent.autoSize = TextFieldAutoSize.LEFT;
			_txtTitle.selectable=false;
			_txtContent.selectable=false;
			_txtTitle.y=txtTitleY;
			_txtContent.y=txtContentY;
			addChild(_txtTitle);
			addChild(_txtContent);
		}
		public function setArr(arr:Array,arr2:Array,arr3:Array):void{
			picArr=arr;
			titleArr=arr2;
			contentArr=arr3;
			pollNum=picArr.length;
			currentNum=0;//读取第一章图�?
			pollShow();//开始poll
			addChild(lightBall);
			lightBall.addEvent();
			lightBall.y = lightBallY;
			lightBall.setBallSize(pollNum,true);
			lightBall.addEventListener(StaticInfo.LEFT_BALL,onLeft);
			lightBall.addEventListener(StaticInfo.RIGHT_BALL,onRight);
		}
		private function onLeft(e:Event=null):void{
			_currentNum--;
			if(_currentNum<0){
				currentNum=pollNum-1;
			}else{
				currentNum=_currentNum;
			}
		}
		private function onRight(e:Event=null):void{
			_currentNum++;
			if(_currentNum>=pollNum){
				currentNum=0;
			}else{
				currentNum=_currentNum;
			}
		}
		/**
		 * 显示图片
		 * @param url	图片url
		 * @param num	当前图片在数组中的编�?
		 * 
		 */		
		private function showPic(url:String):void{
			var loader:LoadPic=new LoadPic();
			loader.name="loader";
			txtTitle=titleArr[_currentNum];
			txtContent=contentArr[_currentNum];
			loader.load(url);
			if (isFirst) {
				isFirst=!isFirst;
				loader.addEventListener(Event.COMPLETE, onComplete);
			}
			addChild(loader);
		}
		private function onComplete(e:Event):void {
			//ͼƬ��ȡ���?�׳��¼�
			var loader:LoadPic=getChildByName("loader") as LoadPic;
			loader.removeEventListener(Event.COMPLETE, onComplete);
			var event:EventZheng = new EventZheng(StaticInfo.JPGLOADERED);
			event.obj.p = new Point(0, txtTitleY);
			dispatchEvent(event);
			
		}
		private function pollShow():void{
			timer.addEventListener(TimerEvent.TIMER,onTimer);
			timer.start();
		}
		private function delLoader():void {
			if(getChildByName("loader")){
				TweenMax.to(getChildByName("loader"), 1, {blurFilter:{blurX:20},autoAlpha:0,onComplete:onTweenCom});
				lightBall.mouseEnabled=false;//锁住lightBall，缓动结束后开启
				lightBall.mouseChildren=false;
				//removeChild(getChildByName("loader"));
			}
		}
		private function onTweenCom():void{
			if(getChildByName("loader")){
				removeChild(getChildByName("loader"));
				lightBall.mouseEnabled=true;
				lightBall.mouseChildren=true;
			}
		}
		public function pollStop():void {
			if (lightBall.parent) removeChild(lightBall);
			lightBall.setBallSize(pollNum,false);
			_txtContent.text = "";
			_txtTitle.text = "";
			onTweenCom();
			timer.stop();
			timer.removeEventListener(TimerEvent.TIMER,onTimer);
			isFirst = true;
			
			lightBall.removeEventListener(StaticInfo.LEFT_BALL,onLeft);
			lightBall.removeEventListener(StaticInfo.RIGHT_BALL,onRight);
			lightBall.delThis();
		}
		private function onTimer(e:TimerEvent=null):void{
			/*_currentNum++;
			if(_currentNum>=pollNum){
				currentNum=0;
			}else{
				currentNum=_currentNum;
			}
			*/
			
		}
		public function set currentNum(value:int):void{
			_currentNum=value;
			delLoader();
			showPic(picArr[value]);
			lightBall.currentBall = value;
		}
		public function get currentNum():int{
			return _currentNum;
		}
		public function set txtTitle(value:String):void{
			_txtTitle.text=value;
		}
		public function set txtContent(value:String):void{
			_txtContent.text=value;
		}
	}
}